<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function login()
    {
        if (IS_POST) {
            $phone = I('post.phone');
            $password = md5(I('post.password'));

            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $phone);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }

             $res = M('gy_user')->where(['phone'=>$phone,'password'=>$password])->find();
             if ($res) {
                //写入session
                $_SESSION["id"] = $res['id'];
                $_SESSION["name"] = $res['name'];
                $_SESSION["phone"] = $res['phone'];

                $url = U('index/index');
                $arr = array(
                    'error' => 1,
                    'info' => '登陆成功',
                    'url' => $url
                    );
                echo json_encode($arr);
             }else {
                $arr = array(
                    'error' => 0,
                    'info' => '账号密码错误'
                    );
                echo json_encode($arr);
             }

        }else {
            $this->display();
        }
        
    }
    //注册
    public function zhuce()
    {
        if (IS_POST) {
            $data['name'] = I('post.names');
            $data['phone'] = I('post.phone');

            $send_to = I('post.yzm');
            $data['password'] = md5(I('post.password'));
            $data['time'] = time();
            //该手机是否注册
            $is_phone = M('gy_user')->where(['phone'=>$data['phone']])->getField('phone');
            if ($is_phone) {
                $arr = array(
                    'error' => 0,
                    'info' => '该手机已注册过'
                    );
                echo json_encode($arr);
                exit;
            }

            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }
            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '验证码错误'
                    );
                echo json_encode($arr);
                exit;
            }

            //入库
            $res = M('gy_user')->add($data);
            if ($res) {
                //写入session
                $_SESSION["id"] = $res;
                $_SESSION["name"] = $data['name'];
                $_SESSION["phone"] = $data['phone'];
                //清除验证码
                unset($_SESSION["sj_mobile"]);

                $url = U('index/index');
                $arr = array(
                    'error' => 1,
                    'info' => '注册成功',
                    'url' => $url
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '注册失败'
                    );
                echo json_encode($arr);
            }

        }else {
            $_SESSION["sj_mobile"] = 6488;
            $this->display();
        }
        
    }

    //用户中心
    public function yhzx()
    {   
        $rztype = M('gy_rz_ger')->where(array('uid'=>$_SESSION['id']))->getField('rztype');
        $this->assign('rztype',$rztype);
        
        $this->display();
    }

    //个人资料
    public function datum()
    {
        $userinfo = M('gy_user')->where(['id'=>$_SESSION['id']])->find();
        $this->assign('userinfo',$userinfo);

        $this->display();
    }

    //头像
    public function face()
    {
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      'Uploads/'; // 设置附件上传目录    
        // 上传文件     
        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息         
            $arr = array(
            'code' => 0,
            'msg' => $upload->getError(),
            );
            echo json_encode($arr);
 
        }else{// 上传成功   
            $info = current($info);//减维 
            //绝对路径
            $url =  __ROOT__.'/'.'Uploads/'.$info['savepath'].$info['savename'];   
            //相对路径
            $path = '/'.'Uploads/'.$info['savepath'].$info['savename'];
            //不是默认 删除旧图片
            $oldpath = M('gy_user')->where(array('id'=>$_SESSION['id']))->getField('face');
            if ($oldpath != '/Uploads/face/face.png') {
                unlink('./'.$oldpath);
            }
            
            //入库
            M('gy_user')->where(array('id'=>$_SESSION['id']))->setField('face',$path);

            $arr = array(
            'code' => 0,
            'msg' => '上传成功',
            'data'=> array(
                'src' => $url,
                'title' => $info['savename']
                )
            );
            echo json_encode($arr);
        }
    }

    //姓名
    public function names()
    {
        if (IS_POST) {
            $name = I('post.names');

            $res = M('gy_user')->where(['id'=>$_SESSION['id']])->setField('name',$name);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '修改成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '修改失败'
                    );
                echo json_encode($arr);
            }

        }
    }
    //密码
    public function newpass()
    {
        if (IS_POST) {
            $newpass = md5(I('post.newpass'));

            $res = M('gy_user')->where(['id'=>$_SESSION['id']])->setField('password',$newpass);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '修改成功'
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '修改失败'
                    );
                echo json_encode($arr);
            }

        } 
    }

    //退出登陆
    public function logout()
    {
        session_unset();
        session_destroy();
        $this->redirect('index/index');
    }

    //忘记密码
    public function pass()
    {
        if (IS_POST) {
            $data['phone'] = I('post.phone');
            $send_to = I('post.yzm');
            $data['password'] = md5(I('post.password'));

            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }
            //短信随机码验证
            if ($send_to != $_SESSION['sj_mobile']) {
                $arr = array(
                    'error' => 0,
                    'info' => '验证码错误'
                    );
                echo json_encode($arr);
                exit;
            }

            //重置密码
            $res = M('gy_user')->where(['phone'=>$data['phone']])->setField('password',$data['password']);
            if ($res) {
                //清除验证码
                unset($_SESSION["sj_mobile"]);

                $url = U('user/login');
                $arr = array(
                    'error' => 1,
                    'info' => '重置成功',
                    'url' => $url
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '重置失败'
                    );
                echo json_encode($arr);
            }
        }else {

           $this->display(); 
        }

    }

    //发起捐
    public function fqj()
    {
        //项目
        $sql = "select t1.* , t2.xm_name,t2.xm_img from gy_faqi as t1 inner join gy_xm as t2 on t1.xm_id=t2.id where t1.uid='{$_SESSION['id']}'";
        $faqis = M()->query($sql);
        $this->assign('faqis',$faqis);

        //循环先加 总筹数 总人数
        $sum = 0;
        $rs_count = 0;
        foreach ($faqis as $v) {
            $sum += $v['fq_num'];
            $rs_count += re_fqcount($v['id']);
        }
        $this->assign('sum',$sum);
        $this->assign('rs_count',$rs_count);
        
        //项目数量
        $count = M('gy_faqi')->where(['uid'=>$_SESSION['id']])->count();
        $this->assign('count',$count);

        $this->display();
    }

    //我的创建
    public function myxm()
    {   
        //名下所有项目
        $myxms = M('gy_xm')->where(['uid'=>$_SESSION['id']])->select();
        $this->assign('myxms',$myxms);
        //项目总数
        $xmconut = M('gy_xm')->where(['uid'=>$_SESSION['id']])->count();
        $this->assign('xmconut',$xmconut);
        //未上线总数
        $wsxconut = M('gy_xm')->where(['uid'=>$_SESSION['id'],'is_online'=>0,'is_over'=>1])->count();
        $this->assign('wsxconut',$wsxconut);
        //上线总数
        $sxconut = M('gy_xm')->where(['uid'=>$_SESSION['id'],'is_online'=>1,'is_over'=>1])->count();
        $this->assign('sxconut',$sxconut);
        //结束总数
        $is_over = M('gy_xm')->where(['uid'=>$_SESSION['id'],'is_over'=>0])->count();
        $this->assign('is_over',$is_over);

        $this->display();
    }
    //我的捐赠
    public function myjz()
    {
        $myjzs = M('gy_wuping')->field('gy_wuping.*,t2.xm_name,t2.xm_img')->join('gy_xm as t2 ON gy_wuping.xm_id=t2.id')->where(['gy_wuping.uid'=>$_SESSION['id']])->select();
        $this->assign('myjzs',$myjzs);
        $this->display();
    }
    //我的礼品
    public function mylj()
    {
        $myljs = M('gy_myljs')->where(['uid'=>$_SESSION['id']])->select();
        $this->assign('myljs',$myljs);

        $this->display();
    }

    public function ismylj()
    {
        if (IS_POST) {
            $id = I('post.id');
            $sytime = time();
            $data = array('is_sy'=>1,'sytime'=>$sytime);
            $res = M('gy_myljs')->where(['id'=>$id])->setField($data);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '使用成功',
                    );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '使用失败'
                    );
                echo json_encode($arr);
            }
        }
    }










}